package com.huangjian.localproject.Mockito;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyString;

/**
 * @author jianhuang
 * @create 2020/6/10
 */
@RunWith(MockitoJUnitRunner.class)
public class Test01 {

    private AccountDao accountDao;

    private AccountServiceImpl accountService;

    @Before
    public void setUp() {
        this.accountDao = Mockito.mock(AccountDao.class);
        this.accountService = new AccountServiceImpl(accountDao);
    }

    @Test
    public void testLoginSuccess() {
        Account account = new Account();
        Mockito.when(accountDao.getAccount(anyString(), anyString())).thenReturn(account);

        assertThat(accountService.getAccountInfo("zhangSan", "123456"), equalTo("index"));
    }

    @Test
    public void testLoginFail() {
        Mockito.when(accountDao.getAccount(anyString(), anyString())).thenReturn(null);
        assertThat(accountService.getAccountInfo("zhangSan", "123456"), equalTo("login"));
    }

    @Test
    public void testLoginException() {
        Mockito.when(accountDao.getAccount(anyString(), anyString())).thenThrow(new UnsupportedOperationException());
        assertThat(accountService.getAccountInfo("zhangSan", "123456"), equalTo("505"));
    }

}
